package com.lelouch.controller;

import com.google.common.collect.Maps;
import com.lelouch.bean.SysUser;
import com.lelouch.beans.PageQuery;
import com.lelouch.beans.PageResult;
import com.lelouch.common.JsonData;
import com.lelouch.param.UserParam;
import com.lelouch.service.SysRoleService;
import com.lelouch.service.SysTreeService;
import com.lelouch.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * 系统用户控制器
 *
 * @author lcy
 * @create 2018-06-19 17:16
 */
@Controller
@RequestMapping("/sys/user")
@Slf4j
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysTreeService sysTreeService;

    @Autowired
    private SysRoleService sysRoleService;

    @RequestMapping("/noAuth.page")
    public ModelAndView noAuth() {
        return new ModelAndView("noAuth");
    }
    /**
     * 新增用户信息
     *
     * @param
     * @return
     */
    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData save(UserParam userParam) {
        sysUserService.save(userParam);
        return JsonData.success("新添用户成功");
    }

    /**
     * 更新用户信息
     *
     * @param userParam
     * @return
     */
    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData update(UserParam userParam) {
        sysUserService.update(userParam);
        return JsonData.success("更新用户成功");
    }


    @RequestMapping("/page.json")
    @ResponseBody
    public JsonData pageByDeptId(@RequestParam("deptId") Integer deptId, PageQuery pageQuery) {
        PageResult<SysUser> pageResult = sysUserService.pageByDeptId(deptId,pageQuery);
        return JsonData.success(pageResult);
    }

    @RequestMapping("/acls.json")
    @ResponseBody
    public JsonData acls(@RequestParam("userId") Integer userId) {
        Map map = Maps.newHashMap();
        map.put("acls",sysTreeService.userAclTree(userId));
        map.put("roles",sysRoleService.getRoleListByUserId(userId));
        return JsonData.success(map);
    }
}
